package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.asn1.axdr.AxdrOptional;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * READRETOMDDATA类
 * 实现读取返回对象方法数据的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class READRETOMDDATA implements AxdrType {

    private byte[] dataCode = null;
    private OMD omd = null;
    private AxdrEnum dar = null;
    private AxdrOptional<Data> retData = new AxdrOptional<Data>(new Data(), false);
    private OAD oad = null;
    private Get_Result result = null;

    public READRETOMDDATA() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(OMD omd, AxdrEnum dar, Data retData, OAD oad, Get_Result result) {
        this.omd = omd;
        this.dar = dar;
        this.retData.setValue(retData);
        this.oad = oad;
        this.result = result;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.omd = new OMD();
        codeLength += this.omd.decode(input);

        this.dar = new AxdrEnum();
        this.dar.setConst();
        codeLength += this.dar.decode(input);

        this.retData = new AxdrOptional<Data>(new Data(), false);
        try {
            codeLength += this.retData.decode(input);
        } catch (java.io.IOException e) {
            throw new RuntimeException("IO exception during decode", e);
        }

        this.oad = new OAD();
        codeLength += this.oad.decode(input);

        this.result = new Get_Result();
        codeLength += this.result.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.oad != null && this.dar != null && this.omd != null && this.retData != null
                    && this.result != null) {
                codeLength = 0;
                try {
                    codeLength += this.result.encode(output);
                    codeLength += this.oad.encode(output);
                    codeLength += this.retData.encode(output);
                    codeLength += this.dar.encode(output);
                    codeLength += this.omd.encode(output);
                } catch (java.io.IOException e) {
                    throw new RuntimeException("IO exception during encode", e);
                }
            } else {
                throw new RuntimeException("omd || dar || retData || oad || result is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding READRETOMDDATA", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.omd != null && this.dar != null && this.retData != null && this.oad != null && this.result != null) {
            return "sequence: {" + "omd: " + this.omd + ", dar: " + this.dar + ", retdata: " + this.retData + ", oad: "
                    + this.oad + ", result: " + this.result + "}";
        } else {
            return "omd || dar || retData || oad || result is null";
        }
    }

    /**
     * 获取omd字段
     * 
     * @return OMD类型的omd字段
     */
    public OMD getOmd() {
        return this.omd;
    }

    /**
     * 获取dar字段
     * 
     * @return AxdrEnum类型的dar字段
     */
    public AxdrEnum getDar() {
        return this.dar;
    }

    /**
     * 获取retData字段
     * 
     * @return AxdrOptional<Data>类型的retData字段
     */
    public AxdrOptional<Data> getRetData() {
        return this.retData;
    }

    /**
     * 获取oad字段
     * 
     * @return OAD类型的oad字段
     */
    public OAD getOad() {
        return this.oad;
    }

    /**
     * 获取result字段
     * 
     * @return Get_Result类型的result字段
     */
    public Get_Result getResult() {
        return this.result;
    }
}